//
//  MMButton.m
//  EraPig
//
//  Created by Mac_Mini on 2021/2/24.
//

#import "MMButton.h"

@implementation MMButton

/// 常规的一种初始化方法
/// @param buttonType 按钮类型
/// @param frame frame
/// @param titleColor 字体颜色
/// @param size 字体大小
+ (MMButton *)buttonWithType:(UIButtonType)buttonType
                       frame:(CGRect)frame
                  titleColor:(UIColor *)titleColor
                   titleSize:(CGFloat)size{
    MMButton *button = [MMButton buttonWithType:buttonType];
    button.frame = frame;
    [button setTitleColor:titleColor forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:size];
    return button;
}

/// 常规的一种初始化方法
/// @param buttonType 按钮类型
/// @param titleColor 字体颜色
/// @param size 字体大小
+ (MMButton *)buttonWithType:(UIButtonType)buttonType
                  titleColor:(UIColor *)titleColor
                   titleSize:(CGFloat)size{
    MMButton *button = [MMButton buttonWithType:buttonType];
    [button setTitleColor:titleColor forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:size];
    return button;
}

/// 渐变色初始化方法
/// @param buttonType 按钮类型
/// @param frame frame
/// @param titleColor 字体颜色
/// @param size 字体大小
/// @param gradientColors 渐变色颜色数组
/// @param startPoint 渐变开始坐标
/// @param endPoint 渐变结束坐标
/// @param locations 变化位置
+ (MMButton *)buttonWithType:(UIButtonType)buttonType
                       frame:(CGRect)frame
                  titleColor:(UIColor *)titleColor
                   titleSize:(CGFloat)size
          gradientBackground:(NSArray <UIColor *>*)gradientColors
                  startPoint:(CGPoint)startPoint
                    endPoint:(CGPoint)endPoint
                   locations:(NSArray <NSNumber *>*)locations{
    MMButton *button = [MMButton buttonWithType:buttonType];
    button.frame = frame;
    [button setTitleColor:titleColor forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:size];
    
    UIGraphicsBeginImageContextWithOptions(frame.size, YES, 1.0);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CAGradientLayer *gl = [CAGradientLayer layer];
    gl.frame = button.bounds;
    gl.startPoint = startPoint;
    gl.endPoint = endPoint;
    gl.colors = gradientColors;
    gl.locations = locations;
    [gl renderInContext:context];
    
    UIImage *backgroundImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [button setBackgroundImage:backgroundImage forState:UIControlStateNormal];
    return button;
}
@end
